<template>
  <div>
    <div class="head">
      <van-icon class="arrow-left" name="arrow-left" @click="goBack" />
      <p>注册</p>
    </div>
    <van-field
      v-model="sms"
      clearable
      @input="getYzm"
      placeholder="请输入短信验证码"
    >
      <template #button>
        <van-button size="small" type="info" color="red" round @click="telCode"
          >发送验证码
        </van-button>
      </template>
    </van-field>
    <van-button
      size="large"
      color="red"
      round
      :disabled="disabled"
      clearable
      type="primary"
      @click="goReg"
      class="button-text"
      >下一步
    </van-button>
  </div>
</template>

<script>
export default {
  name: "Regcode",
  data() {
    return {
      sms: "",
      tel: "",
      msg: "",
      disabled: true,
    };
  },
  created() {
    console.log(this.$route.query.tel);
    this.tel = this.$route.query.tel;
  },
  methods: {
    goBack() {
      this.$router.back();
    },
    // 发送短信验证码
    telCode() {
      this.$server({
        url: "api/user/dosendmsgcode",
        method: "post",
        data: {
          tel: this.tel,
        },
      }).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          this.msg = res.data.message;
        }
      });
    },

    getYzm() {
      if (this.sms.length == 5) {
        this.disabled = false;
      } else {
        this.disabled = true;
      }
    },
    // 验证短信码
    goReg() {
      this.$server({
        url: "/api/user/docheckcode",
        method: "post",
        data: {
          tel: this.tel,
          telcode: this.sms,
        },
      }).then((res) => {
        if (res.data.code == 200) {
          console.log(res);
          this.$router.push(`/Regpass?tel=${this.tel}`);
        }
      });
    },
  },
};
</script>

<style scoped>
.head {
  height: 45px;
  background-color: antiquewhite;
  text-align: center;
  line-height: 45px;
}
.arrow-left {
  float: left;
  margin: 1rem;
  color: blue;
}
p {
  text-align: center;
  margin-right: 42px;
}
.button-text {
  margin-top: 1.8rem;
  width: 20rem;
  height: 2.5rem;
  border-radius: 25px;
  margin-left: 1.2rem;
}
</style>
